package com.dayu.web.controller.share;

import java.util.List;

import com.dayu.share.domain.ShareGoodsPrice;
import com.dayu.share.service.IShareGoodsPriceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.share.domain.ShareGoods;
import com.dayu.share.service.IShareGoodsService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author HK
 * @date 2021-04-08
 */
@RestController
@RequestMapping("/share/goods")
public class ShareGoodsController extends BaseController
{
    @Autowired
    private IShareGoodsService shareGoodsService;
    @Autowired
    private IShareGoodsPriceService goodsPriceService;

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('share:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShareGoods shareGoods)
    {
        startPage();
        List<ShareGoods> list = shareGoodsService.selectShareGoodsList(shareGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('share:goods:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShareGoods shareGoods)
    {
        List<ShareGoods> list = shareGoodsService.selectShareGoodsList(shareGoods);
        ExcelUtil<ShareGoods> util = new ExcelUtil<ShareGoods>(ShareGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('share:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(shareGoodsService.selectShareGoodsById(goodsId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('share:goods:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareGoods shareGoods)
    {
        shareGoods.setHasShow(2);
        return toAjax(shareGoodsService.insertShareGoods(shareGoods));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('share:goods:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareGoods shareGoods)
    {
        return toAjax(shareGoodsService.updateShareGoods(shareGoods));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('share:goods:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        for (Long id : goodsIds) {
            ShareGoodsPrice goodsPrice = new ShareGoodsPrice();
            goodsPrice.setGoodsId(id);
            List<ShareGoodsPrice> list = goodsPriceService.selectShareGoodsPriceList(goodsPrice);
            if(list.size() > 0) {
                return AjaxResult.error("存在报价列表，请先清空在删除！");
            }
        }
        return toAjax(shareGoodsService.deleteShareGoodsByIds(goodsIds));
    }
}
